package pers.dnf.test.java.springcloud.client.balancer.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author dengfan
 * @version 1.0
 * @description
 * @date 2018/9/12 0012 09:55
 */
@RestController
public class BalancerAPI {
    @Autowired
    LoadBalancerClient loadBalancerClient;
    @Autowired
    RestTemplate restTemplate;

    @RequestMapping(value="/add")
    public String add(){
        ServiceInstance serviceInstance = loadBalancerClient.choose("ops-service-dnf");
        return restTemplate.getForEntity("http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/add?a=1&b=2",String.class).getBody();
    }
}
